import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { AdmItemStoreVO, AdmItemStoreForm, AdmItemStoreQuery, AdmItemStoreSearchVO } from '@/api/general/administration/item/store/types';

/**
 * 查询物品入库列表
 * @param query
 * @returns {*}
 */

export const listAdmItemStore = (query?: AdmItemStoreQuery): AxiosPromise<AdmItemStoreVO[]> => {
  return request({
    url: '/system/admItemStore/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询物品入库详细
 * @param id
 */
export const getAdmItemStore = (id: string | number): AxiosPromise<AdmItemStoreVO> => {
  return request({
    url: '/system/admItemStore/' + id,
    method: 'get'
  });
};

/**
 * 新增物品入库
 * @param data
 */
export const addAdmItemStore = (data: AdmItemStoreForm) => {
  return request({
    url: '/system/admItemStore',
    method: 'post',
    data: data
  });
};

/**
 * 修改物品入库
 * @param data
 */
export const updateAdmItemStore = (data: AdmItemStoreForm) => {
  return request({
    url: '/system/admItemStore',
    method: 'put',
    data: data
  });
};

/**
 * 删除物品入库
 * @param id
 */
export const delAdmItemStore = (id: string | number | Array<string | number>) => {
  return request({
    url: '/system/admItemStore/' + id,
    method: 'delete'
  });
};


/**
 * 导入物品 Excel
 * @param file 文件对象
 * @param bookStoreBo 额外参数对象
 */
export const importItems = (file: File, bookStoreBo: AdmItemStoreForm): AxiosPromise<void> => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('data', JSON.stringify(bookStoreBo));

  return request({
    url: '/system/admItemStore/import',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  });
};



/**
 * 查询物品入库列表
 * @param query
 * @returns {*}
 */

export const queryAllItem = (): AxiosPromise<AdmItemStoreVO[]> => {
  return request({
    url: '/system/admItemStore/queryAll',
    method: 'get',
  });
};

/**
 * 模糊查询物品名称
 * @param keyword 关键字
 */
export const getItemList = async (keyword: string): Promise<AdmItemStoreSearchVO[]> => {
  const res = await request({
    url: '/system/admItemStore/search',
    method: 'get',
    params: { keyword }
  })
  // 取 data 数组返回
  return res.data || []
}
